<?php
/**
 * Created by Zend Studio.
 * User: FDG348
 * Date: 2017/9/13
 * Time: 11:36
 */
namespace common\models;

use common\helps\tools;
use yii\db\ActiveRecord;

class ShareWishList extends ActiveRecord{
    public static function tableName(){
        return '{{%share_wish_list}}';
    }

	const TYPE_ONE		=1;//分享二维码
	const TYPE_TWO		=2;//预约到店
	const TYPE_THR		=3;//打印

	public function rules(){
		return [
			[['member_id','goods_ids','type','create_time','share_key'],'safe'],
		];
	}
	/**
	 * @param mod by FDG367 2018-03-20  542 【商城需求】心愿单界面增加预约到店功能，点击预约到店，新增页面填写用户信息 start
	 * @param array $good_ids 商品的id
	 * @param $type
	 * @param $member_id 当前登录用户ID
	 */
	public  function saveShareInfo($good_ids,$type=self::TYPE_TWO,$member_id=0){

		/*//如果存在则返回对应存在的信息 否则保存
		if($returnInfo =self::getShareInfo($good_ids)){
			return $returnInfo;
		}*/

		//不存在则保存对应的信息
		$rand_str = tools::randStr();
		$share_key = $rand_str.microtime(true);;
		$share_val = [
			'goods_ids'		=>json_encode($good_ids),
			'create_time'	=>date('Y-m-d H:i:s'),
			'share_key'		=>$share_key,
			'type'			=>$type,
			'member_id'		=>$member_id
		];

		$this->load($share_val,'');

		if(!$this->save()){
			return false;
		}

		return $this;
	}

	/**
	 * @param 获取已存在信息
	 */
	public static function getShareInfo($goods_ids){
		return self::find()
			->where(['goods_ids'=>json_encode($goods_ids)])
			->one();
	}
}